package cn.tedu.system.user.service.impl;

import cn.tedu.system.base.exception.ServiceException;
import cn.tedu.system.base.response.JsonResult;
import cn.tedu.system.base.response.StatusCode;
import cn.tedu.system.user.mapper.UserMapper;
import cn.tedu.system.user.pojo.dto.UserLoginDto;
import cn.tedu.system.user.pojo.dto.UserLoginTime;
import cn.tedu.system.user.pojo.dto.UserRegDto;
import cn.tedu.system.user.pojo.dto.UserReviseDto;
import cn.tedu.system.user.pojo.entity.User;
import cn.tedu.system.user.pojo.vo.UserVO;
import cn.tedu.system.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    //用户注册功能
    @Override
    public void registerUser(UserRegDto userRegDto) {
        log.debug("用户注册业务: {}",userRegDto);

        if(userMapper.existsByUsername(userRegDto.getUsername())>0){
            throw new ServiceException(StatusCode.USERNAME_EXIST);
        }

        User user = new User();
        BeanUtils.copyProperties(userRegDto, user);
        user.setStatus(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        if(userMapper.reg(user)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //登录功能
    @Override
    public UserVO loginUser(UserLoginDto userLoginDto, HttpSession session) {
        log.debug("userLoginDto: "+userLoginDto);
        UserVO userVO = userMapper.login(userLoginDto);
        if (userVO==null){
            throw new ServiceException(StatusCode.LOGIN_FAILED);
            //return new JsonResult(StatusCode.LOGIN_FAILED);
        }
        //记录登录时间
        UserLoginTime loginTime = new UserLoginTime();
        loginTime.setUsername(userVO.getUsername());
        loginTime.setLastLoginTime(new Date());
        userMapper.loginTime(loginTime);

        session.setAttribute("user",userVO);
        return userVO;
    }

    //个人信息修改功能
    @Override
    public void reviseUser(UserReviseDto userReviseDto, HttpSession session) {
        log.debug("userRegDto: {}", userReviseDto);
        UserVO userVO = (UserVO)session.getAttribute("user");
        if(userVO==null){
            throw new ServiceException(StatusCode.UNAUTHORIZED);
            //return new JsonResult(StatusCode.UNAUTHORIZED);
        }

        User user = new User();
        BeanUtils.copyProperties(userReviseDto, user);
        user.setUsername(userVO.getUsername());
        user.setUpdateTime(new Date());
        if(userMapper.revise(user)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    @Override
    public void logoutUser(HttpSession session) {
        session.removeAttribute("user");
    }

    @Override
    public UserVO selectUser(HttpSession session) {
        UserVO user = (UserVO)session.getAttribute("user");
        if(user==null){
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        UserVO userVO = userMapper.select(user.getId());
        return userVO;
    }
}
